AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

function ENT:Initialize()
	self.Entity:SetModel("models/props_c17/substation_stripebox01a.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.nextthink = CurTime()
	self.scan = false
	self.ejectall = false
	self.ejecttype = ""
	self.entititable = {}
	self.class = {}
	self.valid = {}
	self.maxentititable = 5
	local phys = self.Entity:GetPhysicsObject()  	
	if (phys:IsValid()) then  		
		phys:Wake()  	
	end  
	table.insert(servertable["ship"]["cargoholds"],self.Entity)
end

function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 36
	local ent = ents.Create( "cargo" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Eject()
	local cargoholdentity = self.Entity
	Msg(self.ejecttype)
	if (self.ejecttype != "") then
		Msg("Ejecting")
		local ejectorposes = ents.FindInSphere(self.Entity:GetPos(), 1500 )
		local ejector
		for _,ent in pairs(ejectorposes) do 
			if ent:GetName( ) == "Cargo_Eject" then
				ejector = ent
				break
			end
		end
		if not ejector then 
		Msg("no Exitpoint")
		self.ejecttype = "" 
		return end
		if not ejector:IsValid() then Msg("No exitpoint ") self.ejecttype = "" return else Msg("Spawnpoint found ") end
		if (self.ejectall) then
			for index,ent in pairs(self.class) do 
				if (self.class[index] == self.ejecttype) then
					local 	Spawnent = ents.Create(self.class[index])
					Spawnent:SetAngles(Vector(0,0,0))
					Spawnent:SetPos(ejector:GetPos())
					Spawnent:Spawn()
					Spawnent:Initialize()
					Msg(index .. "Model " .. self.entititable[index] .. " VAlid " .. tostring (self.valid[index]))
					Spawnent:SetModel(self.entititable[index])
					Spawnent.valid = self.valid[index]
					if ( Spawnent:GetPhysicsObject():IsValid() ) then
						Spawnent:Activate()
						local index2 = Spawnent:EntIndex() 
						local plyid = cargoholdentity:EntIndex() 
						servertable["entitys"][index2] = {}
						servertable["entitys"][index2]["owner"] = servertable["entitys"][plyid]["owner"]
						cargoholdentity.entititable[index] = nil
						cargoholdentity.class[index] = nil
						cargoholdentity.valid[index] = nil
						Msg("Finsihed \n")
					end
				end
			end
		else
			for index,ent in pairs(self.class) do 
				if (self.class[index] == self.ejecttype) then
					self.ejecttype = ""
					local 	Spawnent = ents.Create(self.class[index])
					Spawnent:SetAngles(Vector(0,0,0))
					Spawnent:SetPos(ejector:GetPos())
					Spawnent:Spawn()
					Spawnent:Initialize()
					Msg(index .. "Model " .. self.entititable[index] .. " VAlid " .. tostring (self.valid[index]))
					Spawnent:SetModel(self.entititable[index])
					Spawnent.valid = self.valid[index]
					if ( Spawnent:GetPhysicsObject():IsValid() ) then
						Spawnent:Activate()
						local index2 = Spawnent:EntIndex() 
						local plyid = cargoholdentity:EntIndex() 
						servertable["entitys"][index2] = {}
						servertable["entitys"][index2]["owner"] = servertable["entitys"][plyid]["owner"]
						cargoholdentity.entititable[index] = nil
						cargoholdentity.class[index] = nil
						cargoholdentity.valid[index] = nil
						Msg("Finsihed \n")
						break
					end
				end
			end
		end
	end
end
 
function ENT:Scan()
	local entis = ents.FindInSphere(self.Entity:GetPos(), 1000 )
	for _,ent in pairs(entis) do 
		if not(ent == self.Entity) then
			if not (ent:IsConstrained()) then
				if (GetOwnStatus(servertable["entitys"][self.Entity:EntIndex( )]["owner"],ent) < 3) then
					if (table.HasValue(tradegoods,ent:GetClass( ))) then
						local index = 0
						for number = 1, self.maxentititable, 1 do 
							if (self.entititable[number] == nil) then
							index = number
							break
						end
					end
					if (ent:GetClass( ) == "moneydrop") then
						local ply = player.GetByUniqueID(servertable["entitys"][self.Entity:EntIndex()]["owner"])
						Changemoney (ply,ent.amount)
						ent:Remove()
					else
						Msg("Cargoholdindex " .. index)
						if (index != 0) then
							self.class[index] = ent:GetClass( )
							self.valid[index] = ent.valid
							Msg(" Model = " .. ent:GetModel( ) .. "\n")
							self.entititable[index] = ent:GetModel( )
							local pos = ent:GetPos()									
							local effectdata = EffectData() 
								effectdata:SetStart( self.Entity:GetPos() ) 
								effectdata:SetOrigin( pos ) 
								effectdata:SetScale( 200 ) 
								effectdata:SetMagnitude(39)
								effectdata:SetEntity(ent)
							util.Effect( "ToolTracer", effectdata,true,true )  
							local effectdata = EffectData() 
								effectdata:SetStart( self.Entity:GetPos() ) 
								effectdata:SetOrigin( pos ) 
								effectdata:SetScale( 200 ) 
								effectdata:SetMagnitude(39)
								effectdata:SetEntity(ent)
							util.Effect( "ManhackSparks", effectdata,true,true ) 
							ent:Remove()
						end
						end
					end
				end
			end
		end
	end
end

function ENT:OnTakeDamage(info)
	Shieldhit(info,self.Entity)
end